﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.ViewModels.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class AddOrUpdateMeterInfoViewModel:ViewModelBase
    {
        public static MeterInfo MeterInfo;
        private MeterInfoService MeterInfoService = new MeterInfoService();
        public AddOrUpdateMeterInfoViewModel()
        {
            BaudRateList = new ObservableCollection<string>() { "300", "600", "1200", "2400", "4800", "7200", "9600", "19200", "38400", "57600", "115200", "自适应" };
            AgreementTypeList = new ObservableCollection<string>() {
                "DL/T 645-1997" ,
                "DL/T 645-2007",
                "本部分",
                "CT/T 188-2004",
            };
            ConnectTypeList = new ObservableCollection<string>() { "未知", "单相", "三相三线", "三相四线" };
            PortList = new ObservableCollection<string>() { "交采", "485-1", "485-2", "485-3", "载波" };
            
            
        }

        #region 属性

        private int seqNo;

        public int SeqNo
        {
            get { return seqNo; }
            set { seqNo = value; RaisePropertyChanged(); }
        }

        private string address;

        public string Address
        {
            get { return address; }
            set { address = value; RaisePropertyChanged(); }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> baudRateList;

        public ObservableCollection<string> BaudRateList
        {
            get { return baudRateList; }
            set { baudRateList = value; RaisePropertyChanged(); }
        }

        private int rateNum;

        public int RateNum
        {
            get { return rateNum; }
            set { rateNum = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> agreementTypeList;

        public ObservableCollection<string> AgreementTypeList
        {
            get { return agreementTypeList; }
            set { agreementTypeList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> portList;

        public ObservableCollection<string> PortList
        {
            get { return portList; }
            set { portList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> connectTypeList;

        public ObservableCollection<string> ConnectTypeList
        {
            get { return connectTypeList; }
            set { connectTypeList = value; RaisePropertyChanged(); }
        }

        private int userType;

        public int UserType
        {
            get { return userType; }
            set { userType = value; RaisePropertyChanged(); }
        }

        private double voltage;

        public double Voltage
        {
            get { return voltage; }
            set { voltage = value; RaisePropertyChanged(); }
        }

        private double current;

        public double Current
        {
            get { return current; }
            set { current = value; RaisePropertyChanged(); }
        }

        private int baudRateSelected;

        public int BaudRateSlected
        {
            get { return baudRateSelected; }
            set { baudRateSelected = value; RaisePropertyChanged(); }
        }

        private int agreementTypeSelected;

        public int AgreementTypeSlected
        {
            get { return agreementTypeSelected; }
            set { agreementTypeSelected = value; RaisePropertyChanged(); }
        }

        private int portSelected;

        public int PortSelected
        {
            get { return portSelected; }
            set { portSelected = value; RaisePropertyChanged(); }
        }

        private int connectTypeSelected;

        public int ConnectTypeSelected
        {
            get { return connectTypeSelected; }
            set { connectTypeSelected = value; RaisePropertyChanged(); }
        }


        private string tAddress;

        public string TAddress
        {
            get { return tAddress; }
            set { tAddress = value; RaisePropertyChanged(); }
        }

        private string asset;

        public string Asset
        {
            get { return asset; }
            set { asset = value; RaisePropertyChanged(); }
        }

        private string pt;

        public string PT
        {
            get { return pt; }
            set { pt = value; RaisePropertyChanged(); }
        }

        private string ct;

        public string CT
        {
            get { return ct; }
            set { ct = value; RaisePropertyChanged(); }
        }

        #endregion

        public void LoadMeterInfo()
        {
            //MeterInfo = ServiceLocator.Current.GetInstance<MeterInfo>();

            if (MeterInfo == null)
            {
                return;
            }
            SeqNo = MeterInfo.SeqNo.GetValueOrDefault();
            Address = MeterInfo.CommuAddr;
            BaudRateSlected = MeterInfo.Baudrate.GetValueOrDefault();
            AgreementTypeSlected = MeterInfo.ProtocolType.GetValueOrDefault();
            ConnectTypeSelected = MeterInfo.WireMode.GetValueOrDefault();

            switch (MeterInfo.Port)
            {
                case "F2080201":
                    PortSelected = 0;
                    break;
                case "F2010201":
                    PortSelected = 1;
                    break;
                case "F2010202":
                    PortSelected = 2;
                    break;
                case "F2010203":
                    PortSelected = 3;
                    break;
                case "F2090201":
                    PortSelected = 4;
                    break;
                default:
                    break;
            }
            Password = MeterInfo.CommuCode;
            RateNum = Convert.ToInt16(MeterInfo.TarriffNum, 16);
            UserType = Convert.ToInt16(MeterInfo.UserType, 16);
            Voltage = double.Parse(MeterInfo.RatedVoltage);
            Current = double.Parse(MeterInfo.RatedCurrent);

            TAddress = MeterInfo.CollectAddr;
            Asset = MeterInfo.AssetNumber;
            PT = MeterInfo.Pt;
            CT = MeterInfo.Ct;
        }

        public void InitMeterInfo(MeterInfo MeterInfo)
        {
            if (MeterInfo == null)
            {
                return;
            }
            SeqNo = MeterInfo.SeqNo.GetValueOrDefault()+1;
            Address = MeterInfo.CommuAddr;
            BaudRateSlected = MeterInfo.Baudrate.GetValueOrDefault();
            AgreementTypeSlected = MeterInfo.ProtocolType.GetValueOrDefault();
            ConnectTypeSelected = MeterInfo.ProtocolType.GetValueOrDefault();

            switch (MeterInfo.Port)
            {
                case "F2080201":
                    PortSelected = 0;
                    break;
                case "F2010201":
                    PortSelected = 1;
                    break;
                case "F2010202":
                    PortSelected = 2;
                    break;
                case "F2010203":
                    PortSelected = 3;
                    break;
                case "F2090201":
                    PortSelected = 4;
                    break;
                default:
                    break;
            }
            Password = MeterInfo.CommuCode;
            RateNum = Convert.ToInt16(MeterInfo.TarriffNum, 16);
            UserType = Convert.ToInt16(MeterInfo.UserType, 16);
            Voltage = double.Parse(MeterInfo.RatedVoltage);
            Current = double.Parse(MeterInfo.RatedCurrent);

            TAddress = MeterInfo.CollectAddr;
            Asset = MeterInfo.AssetNumber;
            PT = MeterInfo.Pt;
            CT = MeterInfo.Ct;
        }

        public void SaveMeterInfo()
        {
            
            
            MeterInfo.SeqNo = SeqNo;
            MeterInfo.CommuAddr = Address;
            MeterInfo.Baudrate = BaudRateSlected;
            MeterInfo.ProtocolType = AgreementTypeSlected;
            MeterInfo.WireMode = ConnectTypeSelected;
            switch (PortSelected)
            {
                case 0:
                    MeterInfo.Port="F2080201";
                    break;
                case 1:
                    MeterInfo.Port = "F2010201";
                    break;
                case 2:
                    MeterInfo.Port = "F2010202";
                    break;
                case 3:
                    MeterInfo.Port = "F2010203";
                    break;
                case 4:
                    MeterInfo.Port = "F2090201";
                    break;
                default:
                    break;
            }
            MeterInfo.CommuCode = Password;
            MeterInfo.TarriffNum = RateNum.ToString();
            MeterInfo.UserType = UserType.ToString();
            MeterInfo.RatedVoltage = Voltage.ToString();
            MeterInfo.RatedCurrent = Current.ToString();
            MeterInfo.CollectAddr = TAddress;
            MeterInfo.AssetNumber = Asset;
            MeterInfo.Pt = PT;
            MeterInfo.Ct = CT;
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                return new RelayCommand<Window>((w) =>
                {
                    var vm = ServiceLocator.Current.GetInstance<MeterArchivesViewModel>();
                    if (MeterInfo == null)
                    {
                        MeterInfo = new MeterInfo();
                        SaveMeterInfo();
                        if (MeterInfoService.Insert(MeterInfo)>0)
                        {
                            vm.MeterInfoList.Add(MeterInfo);
                            SeqNo += 1;
                            vm.MeterInfoList = new ObservableCollection<MeterInfo>(vm.MeterInfoList.OrderBy(item => item.SeqNo));
                            vm.MeterTotalNumber = vm.MeterInfoList.Count;
                        }
                        
                        MeterInfo = null;
                    }
                    else
                    {
                        SaveMeterInfo();
                        if (MeterInfoService.Update(MeterInfo)>0)
                        {
                            vm.MeterInfoList[vm.SelectedMeterInfo] = MeterInfo;
                        }
                        
                        w.DialogResult = true;

                    }
                });
            }
        }
    }
}
